package com.app.teacherActivity;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.app.NoticeDetail;
import com.app.R;
import com.app.bean.NoticeBean;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class TeacherAdapter extends RecyclerView.Adapter<TeacherAdapter.server> {

    private Context context;
    private List<NoticeBean> list;

    public TeacherAdapter(Context context, List<NoticeBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public server onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.notice_list_item, parent, false);
        return new server(view);
    }

    @Override
    public void onBindViewHolder(server holder, int position) {
        holder.newsTitle.setText(list.get(position).getName());
        holder.newsContent.setText(list.get(position).getContent());
        holder.newsTime.setText("发布时间：" + list.get(position).getTime());
//        List<Integer> bean = new ArrayList<>();
//        bean.add(R.drawable.x1);
//        bean.add(R.drawable.x2);
//        bean.add(R.drawable.x3);
//        bean.add(R.drawable.x4);
//        bean.add(R.drawable.x5);
//        Random random = new Random();
//        int a = random.nextInt(5);
//        holder.imageView.setImageResource(bean.get(a));
        Glide.with(holder.imageView).load(list.get(position).getPic()).into(holder.imageView);
//        list.get(position).setImage(bean.get(a));
        holder.layout.setOnClickListener(v -> {
            //跳转界面，并把数据传递过去，传递数据的名字是bean
            Intent intent = new Intent(context, TeacherDetail.class);
            intent.putExtra("bean", list.get(position));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class server extends RecyclerView.ViewHolder {
        private TextView newsTitle, newsContent, newsTime;
        private LinearLayout layout;
        private ImageView imageView;

        public server(View itemView) {
            super(itemView);
            newsTitle = itemView.findViewById(R.id.newsTitle);
            newsContent = itemView.findViewById(R.id.newsContent);
            newsTime = itemView.findViewById(R.id.newsTime);
            layout = itemView.findViewById(R.id.layout);
            imageView = itemView.findViewById(R.id.newsImage);
        }
    }
}

